package com.baosight.risk.service.xtzy.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baosight.risk.api.xtzy.IXtzyCompanyService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.redis.BaseRedis;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.system.SysRoleMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.mapper.xtzy.XtzyUserMapper;
import com.baosight.risk.service.quartz.entity.SyncOrgEntitiy;
import com.baosight.risk.service.system.entity.SysRole;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import com.baosight.risk.service.xtzy.entity.XtzyOrgEhr;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.vo.XtzyCompanyVO;
import com.google.common.collect.Lists;

import lombok.extern.slf4j.Slf4j;

/**
 * 类 名: XtzyCompanyServiceImpl
 * 描 述:
 * 作 者: 黎秋荣
 * 创 建：2019年04月25日
 * 版 本：v2.2.0
 * 历 史: (版本) 作者 时间 注释
 */
@Service
@Slf4j
public class XtzyCompanyServiceImpl extends BaseServiceImpl<XtzyCompany> implements IXtzyCompanyService {

    @Autowired
    private XtzyCompanyMapper companyMapper;
    @Autowired
    private XtzyUserMapper userMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private BaseRedis redis;

    @Override
    public BaseMapper getMapper() {
        return companyMapper;
    }

    /**
     * 查询组织机构
     *
     * @param company 组织机构
     * @return
     */
    @Override
    public List<XtzyCompany> selectCompany(XtzyCompany company, XtzyUser user) {
        List<XtzyCompany> compList = new ArrayList<>();
        if (StringUtils.isNotEmpty(company.getExt5())) {

            if (StringUtils.isNotEmpty(company.getCompCode())) {
                getCompanyByAuth(company.getExt5(), company.getCompCode(), compList, company,user);
            } else {
                getCompanyByAuth(company.getExt5(), user.getExt1(), compList, company,user);
            }
        } else {
            //根据公司和用户找到所有角色，判断角色中是否有admin角色
            List<SysRole> roleList = roleMapper.selectRoleByCompIdAndUserId(user.getExt1(), user.getId());
            Boolean flag = false;
            if (CollectionUtils.isNotEmpty(roleList)) {
                for (SysRole role : roleList) {
                    if ("system".equals(role.getRoleKey())) {
                        flag = true;
                    }
                }
            }
            if (flag) {
                //全部公司
                getCompanyByAuth("ALL", user.getExt1(), compList, company,user);
            } else {
                //非admin用户查询本公司及以下子公司
                getCompanyByAuth("COMP_SUB", user.getExt1(), compList, company,user);
            }
        }
        return compList;
    }

    public void getCompanyByAuth(String auth, String compCode, List<XtzyCompany> compList, XtzyCompany company,XtzyUser user) {
        //全部
        if ("ALL".equals(auth)) {
            compList.addAll(companyMapper.selectCompany(company));
        } else if ("COMP_SUB".equals(auth)) {
            //本公司及一下公司
            XtzyCompany xtzyCompany = companyMapper.selectByPrimaryKey(compCode);
            if (1 != xtzyCompany.getPathLevel()) {
                //默认当前公司为根节点0
                xtzyCompany.setParentGuid("0");
                //当前公司
                compList.add(xtzyCompany);
                //不是末级，根据当前公司找下级所属公司
                if (xtzyCompany.getEndLevelFlag() != 1) {
                    getNextCompany(xtzyCompany.getId(), compList);
                }
                if (xtzyCompany.getPathLevel() > 1) {
                    getAboveCompany(xtzyCompany.getParentGuid(), compList);
                }
            } else {
                compList.addAll(companyMapper.selectCompany(company));
            }

        } else if("MANAGE_COMP_SUB".equals(auth)) {
            if(!"BGTA".equals(user.getManageCompCode())){
                company.setPathCode(user.getManageCompCode());
                compList.addAll(companyMapper.selectCompanySub(company));
            }else{
                compList.addAll(companyMapper.selectCompany(company));
            }
        }else{
            XtzyCompany xtzyCompany = companyMapper.selectByPrimaryKey(compCode);
            compList.add(xtzyCompany);
        }
    }

    /**
     * 查询全部组织机构
     *
     * @param company 组织机构
     * @return
     */
    @Override
    public List<XtzyCompany> selectAllCompany(XtzyCompany company) {
        return companyMapper.selectAllCompany(company);
    }

    public void getNextCompany(String compId, List<XtzyCompany> companies) {
        List<XtzyCompany> companyList = companyMapper.getCompanyByGuid(compId);
        if (CollectionUtils.isNotEmpty(companyList)) {
            companies.addAll(companyList);
            for (XtzyCompany company : companyList) {
                if (company.getEndLevelFlag() != 1) {
                    this.getNextCompany(company.getId(), companies);
                }
            }
        }
    }

    public void getAboveCompany(String compId, List<XtzyCompany> companies) {
        XtzyCompany company = companyMapper.getCompanyByParentGuid(compId);
        if (null != company) {
            if (company.getPathLevel() == 1) {
                companies.add(company);
            } else {
                companies.add(company);
                this.getAboveCompany(company.getParentGuid(), companies);
            }
        }
    }


    private void buildTree(XtzyCompany company, List<XtzyCompany> companies) {
        for (XtzyCompany item : companies) {
            if (item.getParentGuid().equals(company.getCompCode())) {
                company.getChildren().add(item);
                this.buildTree(item, companies);
            }
        }
    }

    @Override
    public List<XtzyCompany> getOneLevelCompany(String companyCode) {
        //一级子公司
        XtzyCompany company = companyMapper.selectByPrimaryKey(companyCode);
        company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("2"));
        //根据一级子公司找下面的所有数据
        List<XtzyCompany> companyList = companyMapper.getCompanyByGuid(company.getId());
        for (XtzyCompany xtzyCompany : companyList) {
            List<XtzyCompany> childrenList = companyMapper.getCompanyByGuid(xtzyCompany.getId());
            if (!CollectionUtils.isEmpty(childrenList)) {
                xtzyCompany.setChildren(childrenList);
            }

        }
        return companyList;
    }

    @Override
    public List<XtzyCompany> getParantAndChildrens(String companyCode) {
        List<XtzyCompany> companyList = new ArrayList<>();
        //一级子公司
        XtzyCompany company = companyMapper.selectByPrimaryKey(companyCode);
        company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("2"));
        if (company != null) {
            //根据一级子公司找下面的部门
            getNextCompany(company.getCompCode(), companyList);
            companyList.add(company);
        }
        return companyList;
    }

//    public XtzyCompany getOneLevel(String companyCode){
//        XtzyCompany company= companyMapper.selectByPrimaryKey(companyCode);
//        if (company.getPathLevel()>2){
//            company = companyMapper.selectByPrimaryKey(company.getParentGuid());
//            if (company.getPathLevel()>2){
//                getOneLevel(company.getParentGuid());
//            }
//            return company;
//        }
//        return company;
//    }


    @Override
    public List<XtzyCompanyVO> getCompanyAndUsers(String pathLevel, String roleId, Integer userFalg, XtzyUser user) {
        String redisKey = "auth:company:user:" + pathLevel + ":" + roleId + ":" + userFalg;
        //如果缓存中数据已经存在，查询缓存
//        if (redis.isKey(redisKey)){
//            return (List<XtzyCompanyVO>)redis.getValue(redisKey);
//        }
        List<XtzyCompanyVO> listVO = new ArrayList<>();
        //userFalg=0，代表查询所有；userFalg=1，代表查询当前登陆人下一级子公司所有人
        if (userFalg == 1) {
            XtzyCompany company = companyMapper.getCompanyInfo(user.getExt1());
            company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort(pathLevel));
            XtzyCompanyVO companyVO = new XtzyCompanyVO();
            BeanUtils.copyBeanProp(companyVO, company);
            listVO.add(companyVO);
        } else {
            //根据层级找所有的子公司
            listVO = companyMapper.getCompanyByPathLevel(pathLevel);
        }

        if (CollectionUtils.isEmpty(listVO)) {
            return null;
        }
        //根据公司找下面的所属角色人员
        for (XtzyCompanyVO companyVO : listVO) {

            List<XtzyUser> userList = userMapper.getUserByCompAndRole(companyVO.getId(), roleId);
            if (CollectionUtils.isEmpty(userList)) {
                continue;
            }
            companyVO.setUserList(userList);
        }
//        redis.setValue(redisKey,listVO);
        return listVO;

    }

    @Override
    public List<XtzyCompanyVO> getRiskCompany() {
        List<XtzyCompanyVO> companyVOList = new ArrayList<>();
        List<XtzyCompany> companyList = companyMapper.getRiskCompany(new XtzyCompany());
        if (CollectionUtils.isEmpty(companyList)) {
            return null;
        }
        for (XtzyCompany company : companyList) {
            XtzyCompanyVO companyVO = new XtzyCompanyVO();
            BeanUtils.copyBeanProp(companyVO, company);
            //找到这个公司下的风险管理员
            List<XtzyUser> userList = userMapper.getUserByCompAndRole(company.getId(), "711a8217-e9b5-4183-871c-b0e4fb17731c");
            companyVO.setUserList(userList);
            companyVOList.add(companyVO);
        }
        return companyVOList;
    }

    @Override
    public List<XtzyCompany> selectCompanyByRisk(String pathCode) {
        return companyMapper.selectCompanyByRisk(pathCode);
    }

    @Override
    public List<XtzyCompanyVO> getNextComp(String compId, String roleId) {
        XtzyCompany company = companyMapper.selectByPrimaryKey(compId);
        List<XtzyCompanyVO> companyVOList = new ArrayList<>();
        List<XtzyCompany> companies = new ArrayList<>();
        companies.add(company);
        //如果已经不是末级,找下级子公司
        if (company.getEndLevelFlag() != 1) {
            getNextCompany(company.getId(), companies);
        }
        for (XtzyCompany xtzyCompany : companies) {
            XtzyCompanyVO companyVO = new XtzyCompanyVO();
            BeanUtils.copyBeanProp(companyVO, xtzyCompany);
            List<XtzyUser> userList = userMapper.getUserByCompAndRole(xtzyCompany.getId(), roleId);
            companyVO.setUserList(userList);
            companyVOList.add(companyVO);
        }
        return companyVOList;
    }

    @Override
    public XtzyCompanyVO getParentComp(String compId, String roleId) {
        XtzyCompanyVO companyVO = new XtzyCompanyVO();
        XtzyCompany company = companyMapper.selectByPrimaryKey(compId);

        if (1 == company.getPathLevel() || 2 == company.getPathLevel()) {
            BeanUtils.copyBeanProp(companyVO, company);
        } else {
            //获取一级子公司
            company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("2"));
            BeanUtils.copyBeanProp(companyVO, company);
        }
        List<XtzyUser> userList = userMapper.getUserByCompAndRole(companyVO.getId(), roleId);
        companyVO.setUserList(userList);
        return companyVO;
    }


    @Override
    public List<XtzyUser> getUserAndCompIdAndRoleId(String compId, String roleId) {
        String redisKey = "auth:userInfo:" + compId + ":" + roleId;
        //如果缓存中数据已经存在，查询缓存
        if (redis.isKey(redisKey)) {
            return (List<XtzyUser>) redis.getValue(redisKey);
        }
        List<XtzyUser> userList = userMapper.getUserByCompAndRole(compId, roleId);
        redis.setValue(redisKey, userList);
        return userList;
    }

    @Override
    public XtzyCompany getTopCompany(String compCode, Short pathLevel) {
        XtzyCompany company = companyMapper.selectByPrimaryKey(compCode);
//        if (1 == company.getPathLevel() || 2 == company.getPathLevel()) {
        if("1".equals(company.getCompType())){
            return company;
        } else {
            //获取一级子公司
            return companyMapper.getCompByCompCode(company.getPathCode());
        }

    }

    @Override
    public XtzyCompany getTop1CompanyByUser(String compCode) {
        XtzyCompany company = companyMapper.selectByPrimaryKey(compCode);
        if (1 == company.getPathLevel()) {
            return company;
        } else {
            return companyMapper.getTop1CompanyByUser(company.getParentGuid());
        }
    }


    /**
     * 根据用户查找组织
     *
     * @param userCode
     * @return
     */
    @Override
    public List<XtzyCompany> getCompany(String userCode) {
        return companyMapper.getCompany(userCode);
    }

    /**
     * 根据id禁用组织机构
     *
     * @param id 组织机构ID
     * @return
     */
    @Override
    public Integer deleteById(String id) {
        return companyMapper.deleteById(id);
    }

    /**
     * @description 根据公司ID信息获取全部子公司部门
     * @author CuiShiHao
     * @date 2019/8/30
     */
    @Override
    public List<XtzyCompany> selectAllSubsetByParentID(String parentGuid) {
        return companyMapper.selectAllSubsetByParentID(parentGuid);
    }

    /**
     * 更新公司启用禁用状态
     */
    @Override
    public Integer updateCompanyStatus(List<String> ids, String status) {
        Integer result = companyMapper.updateCompanyStatus(ids, status);
        return result;
    }

    @Override
    public List<SyncOrgEntitiy> getAllOrgFromOtherDB(SyncOrgEntitiy syncOrgEntitiy) {
        List<SyncOrgEntitiy> result = companyMapper.getAllOrgFromOtherDB(syncOrgEntitiy);
        return result;
    }

    @Override
    public void updateSortCompany() {
        XtzyCompany company = this.get("BGTA");
        company.setRowOrder("001");
        company.setPathName("/" + company.getCompName());

        List<XtzyCompany> companyList = new ArrayList<>();
        companyList.add(company);

        // 递归处理List
        List<XtzyCompany> resultList = sortSonCompany(company);
        companyList.addAll(resultList);

        companyMapper.updateCompanyList(companyList);
    }

    @Override
    public List<XtzyCompany> getCompByCompType(String compType) {
        return companyMapper.getCompByCompType(compType);
    }

    @Override
    public List<XtzyUser> getUserByCompAndRole(String compCode, String roleCode) {
        return companyMapper.getUserByCompAndRole(compCode, roleCode);
    }

    /**
     * 查询本单位及下属所有子公司
     *
     * @param xtzyCompany
     * @return
     */
    @Override
    public List<XtzyCompany> queryCompany(XtzyCompany xtzyCompany) {
        List<XtzyCompany> companyList = Lists.newArrayList();
        List<XtzyCompany> companies = companyMapper.selectAllCompany(xtzyCompany);
        companyList.addAll(companies);
        for (XtzyCompany company : companies) {
            if (StringUtils.isNotEmpty("" + company.getEndLevelFlag())) {
                if(company.getEndLevelFlag() != 1){
                    getNextCompany(company.getId(), companyList);
                }
            }
        }
        return companyList;
    }

    @Override
    public Integer updateCompany(XtzyCompany company) throws Exception {
        company = (XtzyCompany) BeanUtils.updateBean(company);

        if (StringUtils.isEmpty(company.getParentGuid())) {
            throw new Exception("未找到父节点");
        }
        XtzyCompany parentCompany = companyMapper.selectByPrimaryKey(company.getParentGuid());

        // 修改pathName
        String oldName = company.getPathName();
//        String newName = (company.getPathName().substring(0, company.getPathName().lastIndexOf("/") + 1)) + company.getCompName();
        String newName = parentCompany==null? "/" +company.getCompName() : parentCompany.getPathName() + "/" + company.getCompName();

        // 修改pathCode
        String oldCode = company.getPathCode();
        String newCode = parentCompany==null? "/" +company.getCompCode() :parentCompany.getPathCode() + "/" + company.getCompCode();

        company.setPathName(newName);
        company.setPathCode(newCode);
        log.info("修改组织机构信息：" + company.toString());
        int update = companyMapper.updateByPrimaryKeySelective(company);
        if (update > 0) {
            List<XtzyCompany> companyList = companyMapper.selectAllSubsetByParentID(company.getCompCode());
            if (CollectionUtils.isNotEmpty(companyList)) {
                List<String> compCodes = companyList.stream().map(XtzyCompany::getCompCode).collect(Collectors.toList());
                update += companyMapper.updateCompName(compCodes, oldName, newName, oldCode, newCode);
            }
            return update;
        } else {
            throw new Exception("修改组织机构信息失败");
        }
    }

    // 递归处理子部门信息
    private List<XtzyCompany> sortSonCompany(XtzyCompany parentCompany) {
        List<XtzyCompany> resultList = new ArrayList<>();

        // 处理子部门RowOrder递归增加
        int orderNum = 1;
        XtzyCompany infoCompany = new XtzyCompany();
        infoCompany.setParentGuid(parentCompany.getId());
        List<XtzyCompany> companyList = companyMapper.selectAllCompany(infoCompany);
        for (XtzyCompany company : companyList) {
            // 重新拼接OrderNum
//            String RowOrder = Strings.padStart(String.valueOf(orderNum), 3, '0');
            company.setRowOrder(parentCompany.getRowOrder() + orderNum);
            //System.out.println(parentCompany.getRowOrder() + RowOrder);
            orderNum++;
            // 重新拼接PathName
            company.setPathName(parentCompany.getPathName() + "/" + company.getCompName());
            // 重新修改层级
            int level = parentCompany.getPathLevel() + 1;
            company.setPathLevel((short) level);
            resultList.add(company);
            resultList.addAll(sortSonCompany(company));
        }
        return resultList;
    }


    @Override
    public Map<String, Object> selectOrgTree(String type) {
        List<Map<String, Object>> nodeMaps = new ArrayList<>();
        return generateEHRTree(companyMapper.selectOrgEHR(), type, nodeMaps);
    }

    @Override
    public void selectOrgEhr(String type, List<Map<String, Object>> nodeMaps) {
        generateEHRTree(companyMapper.selectOrgEHR(), type, nodeMaps);
    }

    @Override
    public List<XtzyCompany> selectCompanyList(String compCode) {
        return companyMapper.selectCompanyList(compCode);
    }

    /**
     * 生成EHR的组织树
     */
    private Map<String, Object> generateEHRTree(List<XtzyOrgEhr> xtzyOrgEhrList, String type, List<Map<String, Object>> nodeMaps) {
        Map<String, Object> treeMap = new HashMap<>();
        if ("1".equals(type)) {
            // 根节点
            XtzyOrgEhr treeRoot = getTreeRoot(xtzyOrgEhrList.get(0), xtzyOrgEhrList);
            treeMap.put("label", treeRoot.getOrgCode() + "-" + treeRoot.getOrgName());
            treeMap.put("orgCode", treeRoot.getOrgCode());
            treeMap.put("orgName", treeRoot.getOrgName());
            treeMap.put("pathCode", treeRoot.getOrgCode() + "\\");
            treeMap.put("pathName", treeRoot.getOrgName() + "\\");
            treeMap.put("pathLevel", 1);
        } else if ("2".equals(type)) {
            // 自定义根节点
            treeMap.put("label", "BGTA-宝山钢铁股份有限公司");
            treeMap.put("orgCode", "BGTA");
            treeMap.put("orgName", "宝山钢铁股份有限公司");
            treeMap.put("pathCode", "BGTA\\");
            treeMap.put("pathName", "宝山钢铁股份有限公司\\");
            treeMap.put("pathLevel", 1);
        }
        Map<String, List<Map<String, Object>>> parentMap = getParentMap(xtzyOrgEhrList);

        // 移除股份总部1、股份总部2
        List<String> removeOrg = new ArrayList<>();
        removeOrg.add("BABC");
        removeOrg.add("BGLJ");

        connectTree(nodeMaps, treeMap, parentMap, type, removeOrg);
        return treeMap;
    }

    /**
     * 向上寻找根节点
     */
    private XtzyOrgEhr getTreeRoot(XtzyOrgEhr current, List<XtzyOrgEhr> xtzyOrgEhrList) {
        for (XtzyOrgEhr xtzyOrgEhr : xtzyOrgEhrList) {
            // 寻找父节点
            if (xtzyOrgEhr.getOrgCode().equals(current.getTargetOrgCode())) {
                return getTreeRoot(xtzyOrgEhr, xtzyOrgEhrList);
            }
        }
        return current;
    }

    /**
     * 把所有公司和部门按照 父 -> (子...) 结构分类
     */
    private Map<String, List<Map<String, Object>>> getParentMap(List<XtzyOrgEhr> xtzyOrgEhrList) {
        Map<String, List<Map<String, Object>>> parentMap = new HashMap<>();
        List<Map<String, Object>> childList = null;
        for (XtzyOrgEhr xtzyOrgEhr : xtzyOrgEhrList) {
            String key = xtzyOrgEhr.getTargetOrgCode();
            Map<String, Object> childMap = BeanUtils.beanToMap(xtzyOrgEhr);
            childMap.put("label", xtzyOrgEhr.getOrgCode() + "-" + xtzyOrgEhr.getOrgName());
            if (parentMap.containsKey(key)) {
                childList = parentMap.get(key);
                childList.add(childMap);
            } else {
                childList = new ArrayList<>();
                childList.add(childMap);
                parentMap.put(key, childList);
            }
        }
        return parentMap;
    }

    /**
     * 根据父子关系拼接树结构
     */
    private void connectTree(List<Map<String, Object>> nodeMaps, Map<String, Object> treeMap, Map<String, List<Map<String, Object>>> parentMap, String type, List<String> removeList) {
        if (parentMap.containsKey(treeMap.get("orgCode"))) {
            List<Map<String, Object>> realChildList = null;
            List<Map<String, Object>> childList = parentMap.get(treeMap.get("orgCode"));
            if ("1".equals(type)) {
                realChildList = childList;
                for (Map<String, Object> childMap : realChildList) {
                    childMap.put("pathCode", "" + treeMap.get("pathCode") + childMap.get("orgCode") + "\\");
                    childMap.put("pathName", "" + treeMap.get("pathName") + childMap.get("orgName") + "\\");
                    String pathName = String.valueOf(childMap.get("pathName"));
                    childMap.put("label", childMap.get("label") + "-------->" + pathName.substring(0, pathName.length()-1));
                    childMap.put("pathLevel", (int)treeMap.get("pathLevel") + 1);
                    nodeMaps.add(childMap);
                }
                treeMap.put("children", realChildList);
            } else if ("2".equals(type)) {
                realChildList = new ArrayList<>();
                treeMap.put("children", realChildList);
                for (Map<String, Object> childMap : childList) {
                    String orgCode = String.valueOf(childMap.get("orgCode"));
                    // 过滤总部：编码长度为6位，且尾号为00
                    if (orgCode.length() == 6 && "00".equals(orgCode.substring(orgCode.length()-2, orgCode.length()))) {
                        if (parentMap.containsKey(orgCode)) {
                            List<Map<String, Object>> headChildMapList = parentMap.get(orgCode);
                            for (Map<String, Object> headChildMap : headChildMapList) {
                                headChildMap.put("targetOrgCode", treeMap.get("orgCode"));
                                headChildMap.put("targetOrgName", treeMap.get("orgName"));
                                headChildMap.put("pathCode", "" + treeMap.get("pathCode") + headChildMap.get("orgCode") + "\\");
                                headChildMap.put("pathName", "" + treeMap.get("pathName") + headChildMap.get("orgName") + "\\");
                                String pathName = String.valueOf(headChildMap.get("pathName"));
                                headChildMap.put("label", headChildMap.get("label") + "-------->" + pathName.substring(0, pathName.length()-1));
                                headChildMap.put("pathLevel", (int)treeMap.get("pathLevel") + 1);
                                realChildList.add(headChildMap);
                                nodeMaps.add(headChildMap);
                            }
                        }
                    } else {
                        boolean flag = true;
                        // 移除股份总部1、股份总部2
                        for (String removeOrg : removeList) {
                            if (removeOrg.equals(orgCode)) {
                                flag = false;
                                break;
                            }
                        }
                        if(flag) {
                            childMap.put("pathCode", "" + treeMap.get("pathCode") + childMap.get("orgCode") + "\\");
                            childMap.put("pathName", "" + treeMap.get("pathName") + childMap.get("orgName") + "\\");
                            String pathName = String.valueOf(childMap.get("pathName"));
                            childMap.put("label", childMap.get("label") + "-------->" + pathName.substring(0, pathName.length()-1));
                            childMap.put("pathLevel", (int)treeMap.get("pathLevel") + 1);
                            realChildList.add(childMap);
                            nodeMaps.add(childMap);
                        }
                    }
                }
            }
            if (CollectionUtils.isNotEmpty(realChildList)) {
                for (Map<String, Object> childMap : realChildList) {
                    connectTree(nodeMaps, childMap, parentMap, type, removeList);
                }
            }
        }
    }

	@Override
	public XtzyCompany getManageCompany(String compCode) {
		XtzyCompany param = new XtzyCompany();
		param.setCompCode(compCode);
		XtzyCompany company = companyMapper.selectOne(param);
		if(company == null){
			return null;
		}
		if("Y".equals(company.getManageFlag())){
			return company;
		}else{
			String parentCode = company.getParentGuid();
			return this.getManageCompany(parentCode);
		}
	}

    @Override
    public boolean checkManageCompCode(String queryManageCompCode,String userManageCompCode) {
        XtzyCompany paramCompany=new XtzyCompany();
        paramCompany.setPathCode(userManageCompCode);
        paramCompany.setManageFlag("Y");
        List<XtzyCompany> companyList=companyMapper.selectAllCompany(paramCompany);
        for (XtzyCompany item :companyList){
            if(item.getCompCode().equals(queryManageCompCode)){
                return true;
            }
        }
        return false;
    }

	@Override
	public List<XtzyCompany> ownerCompList(String compCode) {
		// TODO Auto-generated method stub
		return companyMapper.ownerCompList(compCode);
	}


}
